package org.obeonetwork.dsl.spem.extension.spem_ifest.dashboard;

import ifest.ontology.SPEMOntologyFactory;
import ifest.ontology.OWLIMTool;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.xml.parsers.ParserConfigurationException;


import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EGenericType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.impl.EStructuralFeatureImpl;
import org.eclipse.emf.ecore.resource.Resource;


import com.clarkparsia.owlapiv3.OWL;

public class IFESTImport {
	/*
	String tempFile = "D://Working/ontologies/temp/ifestimport.owl";
	String sqwlrFile = "D://Working/ontologies/lib/sqwrl.owl";
	String swlraFile = "D://Working/ontologies/lib/swrla.owl";
//	ifestMethodContentPackage ifestPackage = null;
	MethodContentPackage ifestTaskPackage = null;
	MethodContentPackage ifestToolPackage = null;
	MethodContentPackage ifestToolTypePackage = null;
	MethodContentPackage ifestArtifactPackage = null;
	EngineeringDomainPackage ifestDomainPackage = null;
	public boolean validate(MethodLibrary lib)
	{
		if (lib!=null)
		return true;
		return false;
	}
public boolean generatePackage(MethodLibrary lib)
{
	if (lib==null)
		return false;
	//create model Factory for create element
	Spem_ifestFactoryImpl ifestfactory = new Spem_ifestFactoryImpl();
	SpemFactoryImpl spemfactory = new SpemFactoryImpl();
	//get MethodContentPackage reference in Model
	MethodContentPackage mcp = lib.getOwnedMethodPlugin().get(0).getOwnedMethodContentPackage().get(0);
	//create ifest package
	ifestPackage = ifestfactory.createifestMethodContentPackage();
	ifestPackage.setName("IfestPackage");
	//create and add the subpackages in ifest package
	MethodContentPackage sub_package = spemfactory.createMethodContentPackage();
	sub_package.setName("Tasks extra information");
	ifestPackage.getTaskExtras().add(sub_package);
	ifestTaskPackage = sub_package;
	sub_package = spemfactory.createMethodContentPackage();
	sub_package.setName("Work productions extra information");
	ifestPackage.getWorkProductionExtras().add(sub_package);
	ifestArtifactPackage = sub_package;
	sub_package = spemfactory.createMethodContentPackage();
	sub_package.setName("Tools extra information");
	ifestPackage.getToolExtras().add(sub_package);
	MethodContentPackage sub_package2 = spemfactory.createMethodContentPackage();
	sub_package2.setName("Tooltypes");
	sub_package.getOwnedMethodContentMember().add(sub_package2);
	ifestToolTypePackage = sub_package2;
	sub_package2 = spemfactory.createMethodContentPackage();
	sub_package2.setName("Tools");
	sub_package.getOwnedMethodContentMember().add(sub_package2);
	ifestToolPackage = sub_package2;
	ifestDomainPackage = ifestfactory.createEngineeringDomainPackage();
	ifestDomainPackage.setName("Engineering Domains");
	ifestPackage.setEngineeringDomains(ifestDomainPackage);
	mcp.getOwnedMethodContentMember().add(ifestPackage);
	return true;
}
public void getIFEST()
	{
		File file = new File (tempFile);
		if (file.exists())
			file.delete();
		String context = "file://C:/fakepath/ifest-rdf.owl";
		OWLIMTool.getOWL(context, tempFile);
	}
	public boolean importToModel( MethodLibrary[] inlib) throws OWLOntologyCreationException, SAXException, IOException, ParserConfigurationException
	{
		MethodLibrary lib =inlib[0];
		if (validate(lib)==false)
			return false;
		List<MethodContentPackageableElement> packages = lib.getOwnedMethodPlugin().get(0).getOwnedMethodContentPackage().get(0).getOwnedMethodContentMember();
		ToolDefinitionPackage toolDefintionPackage = null;
		TaskDefinitionPackage taskDefintionPackage = null;
		Spem_ifestFactoryImpl ifestfactory = new Spem_ifestFactoryImpl();
		SpemFactoryImpl spemfactory = new SpemFactoryImpl();
		for (MethodContentPackageableElement p:packages)
		{
			if (p.getName().equalsIgnoreCase("Standard Categories"))
			{
				List<MethodContentPackageableElement> packages2 = ((MethodContentPackage)p).getOwnedMethodContentMember();
				for (MethodContentPackageableElement p2:packages2)
				{
					if (p2 instanceof ToolDefinitionPackage)
					{
						toolDefintionPackage = (ToolDefinitionPackage) p2;
						break;
					}
				}				
			}
			if (p instanceof TaskDefinitionPackage)
			{
				taskDefintionPackage = (TaskDefinitionPackage) p;
			}
		}
		ifest.ontology.Factory factory = new Factory();
	//	Bundle bundle = Platform.getBundle(Activator.PLUGIN_ID);
	//	URL repository = bundle.getEntry("resource/repositoryInfo.xml");
	//	URL ifestxml = bundle.getEntry("resource/ifestInfo.xml");  
		String repository = "D:/Working/xml/repositoryInfo.xml";
		String ifest = "D:/Working/xml/ifestInfo.xml";
		factory.init(repository, ifest);
		
		ifestDomainPackage.getEngineeringDomains().addAll(factory.getEngineeringDomain());
		ifestToolTypePackage.getOwnedMethodContentMember().addAll(factory.getToolTypes(ifestDomainPackage.getEngineeringDomains()));
		//generate spem tool definition
		for (MethodContentPackageableElement t : ifestToolTypePackage.getOwnedMethodContentMember())
		{
			if (t instanceof ToolType)
			{
				ToolType toolType = (ToolType)t;
				ToolDefinition tool = spemfactory.createToolDefinition();
				tool.setName(toolType.getName());
				toolDefintionPackage.getOwnedMethodContentMember().add(tool);
				toolType.setToolDefinition(tool);
			}
		}
		ifestTaskPackage.getOwnedMethodContentMember().addAll(factory.getTaskDefinitions(ifestDomainPackage.getEngineeringDomains()));
		//generate spem task definition
		for (MethodContentPackageableElement t : ifestTaskPackage.getOwnedMethodContentMember())
		{
			if (t instanceof ifestTaskDefinition)
			{
				ifestTaskDefinition task = (ifestTaskDefinition)t;
				TaskDefinition taskDefintion = spemfactory.createTaskDefinition();
				taskDefintion.setName(task.getName());
				taskDefintionPackage.getOwnedMethodContentMember().add(taskDefintion);
				task.setTaskDefinition(taskDefintion);
			}
		}
		return true;
	}
	*/
}
